<?php
/**
 * Create By: PhpStorm
 * User: yuanxuncheng
 * File: AddGoodsCategory.php
 * Date: 2017/1/2
 * Time: 23:13
 */

namespace app\api\model\page\goods;

use app\api\model\dao\GoodsCategory;
use app\library\Error;
use app\library\PageBase;
use think\Validate;
use think\Cache;

class AddGoodsCategory extends PageBase
{

    private $objGoodsCategory;

    public function __construct()
    {
        parent::__construct();

        $this->objGoodsCategory = new GoodsCategory();
    }

    /**
     * @param $param
     * @return array|bool
     * @DESC 验证接口字段
     */
    public function checkParam($param)
    {
        $rule = [
            'category_name' => 'require',
            'keywords' => 'require',
            'cat_desc' => 'require',
        ];
        $msg = [
            'category_name.require' => '商品分类名称必填',
            'keywords.require' => '商品分类关键字必填',
            'cat_desc.require' => '商品分类描述必填',
        ];

        $validate = new Validate($rule, $msg);
        $result = $validate->check($param);
        if (!$result) {
            return $validate->getError();
        }
        return '';
    }

    public function excute($param)
    {
        //$ret = $this->checkToken($param);
        //if (false === $ret) {
        //    return $this->getRes(Error::ERR_SYS);
        //}

        // TODO:业务逻辑
        // 1、参数校验
        $ret = $this->checkParam($param);
        if ($ret) {
            return $this->getRes(Error::ERR_PARAM, $ret);
        }

        $category_name = $param['category_name'];
        $keywords = $param['keywords'];
        $cat_desc = $param['cat_desc'];
        $parent_id = 0;
        if (isset($param['parent_id'])) {
            $parent_id = $param['parent_id'];
        }
        $img = '';
        if (isset($param['img'])) {
            $img = $param['img'];
        }

        $ret = $this->objGoodsCategory->addGoodsCategory($category_name, $keywords, $cat_desc, $parent_id, $img);
        if ($ret === false) {
            return $this->getRes(Error::ERR_SYS);
        }

        return $this->getRes();
    }
}
